package org.fuys.coder.domain.act.lottery.service.award.impl;

import org.fuys.coder.domain.act.lottery.model.req.award.AwardAddReq;
import org.fuys.coder.domain.act.lottery.model.req.award.AwardSelectReq;
import org.fuys.coder.domain.act.lottery.model.vo.award.AwardVO;
import org.fuys.coder.domain.audit.model.req.AuditTask;
import org.fuys.coder.domain.audit.model.vo.AuditTaskIdTypeVO;
import org.fuys.coder.domain.audit.model.vo.AuditTaskTypeVO;
import org.fuys.coder.domain.audit.service.Audit;
import org.fuys.coder.domain.act.lottery.repository.IAwardRepository;
import org.fuys.coder.domain.act.lottery.service.award.ILotteryAwardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.lottery.service.award.impl
 * @className: LotteryAwardServiceImpl
 * @author: WangJing
 * @description: 服务实现类
 * @date: 2024/7/8 23:53
 * @version: 1.0
 */
@Service
public class LotteryAwardServiceImpl implements ILotteryAwardService {
    @Resource
    private IAwardRepository awardRepository;

    @Resource(name="dynamicAuditServiceImpl")
    private Audit audit;

    @Transactional
    @Override
    public void addAward(AwardAddReq req,Integer userId) {
        AuditTask auditTask=new AuditTask();
        auditTask.setType(AuditTaskTypeVO.MULTIPLE.getIndex());
        auditTask.setTasks(new String[]{req.getAwardName(),req.getDesc()
                ,req.getContent(),"|",req.getFileKey()});
        auditTask.setUserId(userId);
        auditTask.setIdType(AuditTaskIdTypeVO.INFO.getIndex());
        awardRepository.addAward(req);
        final Long id = req.getId();
        auditTask.setOtherId(id);
        auditTask.setCallback((unused -> {
            awardRepository.updateStatusById(id,1);
            return null;
        }));
        audit.doAudit(auditTask);
    }

    @Override
    public List<AwardVO> getPageAward(AwardSelectReq req) {
        return awardRepository.getPage(req.getPageDTO(),req.getAwardId(),req.getAwardName());
    }
}
